package kr.saeildadim.androidexam_saeildamil.chapter12;

import java.util.ArrayList;

import kr.saeildadim.androidexam_saeildamil.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class Ch12_LiastAddDelEx extends Activity {
	ArrayList<String> arLiast ;
	ArrayAdapter<String> adapter;
	ListView list;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
	
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch12_listadddel);
	
		arLiast= new ArrayList<String>();
		arLiast.add("First");
		arLiast.add("Second");
		arLiast.add("Third");
		
		adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arLiast);
		list = (ListView) findViewById(R.id.ch12_listadddel_list);
		list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		list.setAdapter(adapter);
		list.setOnItemClickListener(new OnItemClickListener() {
				public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
					Toast.makeText(Ch12_LiastAddDelEx.this, arLiast.get(position), Toast.LENGTH_LONG).show();
				
			}
		});
	
	}
	public void mOnClick(View v){
		EditText et = (EditText) findViewById(R.id.ch12_listadddel_edittext);
		switch(v.getId()){
			case R.id.ch12_listadddel_addbnt:
				String str =et.getText().toString();
				arLiast.add(str);
				adapter.notifyDataSetChanged();
				et.setText("");
				break;
			
			case R.id.ch12_listadddel_delbnt:
				int pos = list.getCheckedItemPosition();
				if(pos != ListView.INVALID_POSITION){
					arLiast.remove(pos);
					list.clearChoices();
					adapter.notifyDataSetChanged();
				}
				break;
		
		}
	}
	
}
